#!/bin/bash
USAGE_TEXT=""
_FLAG_LIST=( h )
_LONG_LIST=( help )

add_OPT ()
{
local _tmp_input_flag=false

for OPT in "$@"
do
    local FIELD=${OPT%%=*}
    local VALUE=${OPT#*=}

## DEBUG line
# echo "Option: ${FIELD} has a value of: ${VALUE}"

    case ${FIELD} in
	s|short)
	    local _tmp_short=$(echo ${VALUE} | tr -d [:space:] )
	    local _tmp_short_help="-${VALUE}"
            _FLAG_LIST=(${_FLAG_LIST[@]} ${VALUE})
	    if [ ! "$(echo ${_tmp_short} | tr -d [:space:] |wc -c)" == 1 ]
	    then
	        echo -e "\e[1;31mERROR\e[0m: short option can only be 1 char"
		exit 1
	    fi
	    ;;
	l|long)
	    local _tmp_long=${VALUE//-/_}
	    local _tmp_long_help="--${VALUE}"
            _LONG_LIST=(${_LONG_LIST[@]} ${VALUE})
	    ## ensure no spaces/specials
	    ;;
	d|default)
	    local _tmp_default="${VALUE}"
	    ;;
	b|bool)
	    local _tmp_bool=${VALUE}
	    local _tmp_bool_flag=true
	    if [ ! "${VALUE}"=="true" ] && [ ! "${VALUE}"=="false" } ]
	    then
	        echo -e "\e[1;31mERROR\e[0m: option bool requires: \e[34mtrue or false\e[0m"
		exit 1
	    fi
	    ;;
	h|help)
	    local _tmp_help="${VALUE}"
	    ;;
	i|input)
	    ## input means required value for option
	    ## VALUE here goes to help text for display (if set)
	    local _tmp_input_flag=true
	    local _tmp_input=${VALUE}
	    ;;
	*)
	    ## Unknown option quit.
	    echo -e "\e[1;31mERROR\e[0m: unknown option: \e[34m${FIELD}\e[0m"
	    exit 1
    esac

done


if [ -z ${_tmp_long} ] && [ -z ${_tmp_short} ]
then
    echo -e "\e[1;31mERROR\e[0m: Either long OR short name required."
fi
if [ -z ${_tmp_long} ]
then
    local _tmp_name=FLAG_${_tmp_short}
else
    local _tmp_name=${_tmp_long}
fi
## Debug
#echo "FLAG NAME = ${_tmp_name}"

if [ ! -z ${_tmp_short} ]
then
    eval OPT_FLAG_${_tmp_short}=true
fi

if [ ! -z "${_tmp_default:-""}" ]
then
    eval OPT_DEFAULT_${_tmp_name}="${_tmp_default}"
fi

if ${_tmp_bool_flag}
then
    eval OPT_${_tmp_name}=${_tmp_bool}
    if ${_tmp_bool}
    then
	eval OPT_no${_tmp_name}=false
    else
	eval OPT_no${_tmp_name}=true
    fi
fi

## i|input)
## input means required value for option
## VALUE here goes to help text for display (if set)
if [ ! -z ${_tmp_short} ]
then
    eval OPT_INPUT_FLAG_${_tmp_short}=${_tmp_input_flag}
fi
if [ ! -z ${_tmp_long} ]
then
    eval OPT_INPUT_${_tmp_long//-/_}=${_tmp_input_flag}
fi
TEST_VAL=OPT_INPUT_${_tmp_name}
echo "INPUT FLAG:${TEST_VAL} = ${!TEST_VAL}"
_tmp_input_flag=false

## Build usage/help inside loop to keep info together
if [ ! -z "${_tmp_short}" ]
then
    HELP_Text="${_tmp_short_help}"
    if [ ! -z "${_tmp_long}" ]
    then
        HELP_Text="${HELP_Text}|${_tmp_long_help}"
    fi
else
    HELP_Text="${_tmp_long_help}"
fi
_tmp_tabstop_max=24
_tmp_tabstop=$((${_tmp_tabstop_max}-${#HELP_Text}))
_tmp_space=""
for ((x=1;x<=${_tmp_tabstop};x++))
do
   _tmp_space="${_tmp_space} "
done
eval OPT_HELP_${_tmp_name}=\"${HELP_Text}"${_tmp_space}"${_tmp_help}\"
USAGE_TEXT="${USAGE_TEXT}${HELP_Text}${_tmp_space}${_tmp_help}"
USAGE_TEXT="$(echo -e "${USAGE_TEXT}")<BR>"

## Debug
#echo "Inside"
#echo "OPT_${_tmp_name} = \${OPT_${_tmp_name}}"
}

Usage()
{
    echo "${OPT_DESCRIPTION}"
    echo "$(echo "${USAGE_TEXT}"|sed 's/<BR>/\n/g')"
}

Short_Usage()
{
    echo -e "\n Usage:\n ${0##*/} [ FLAGS ] [ LONG FLAGS ] \n FLAGS: ${_FLAG_LIST[@]}\n LONG FLAGS: ${_LONG_LIST[@]}"
}

parse_opts ()
{
    echo "Parsing command line."
    echo "Command given: ${0##*/}"
    echo "Parameters: ${@}"
    VAL_CONSUME=false

    for VAR in "${@}"
    do
#### echo "Flags = ${#@}"
#### echo "First param = ${1}"
#### echo "Second param= ${2:-Undefined}"
	if ${VAL_CONSUME}
	then
            VAL_CONSUME=false
	    break
        fi
        if [ "${VAR#-}" == "h" ] || [ "${VAR#--}" == "help" ]
        then
            Usage
            exit 0
        fi
        echo "Read variable: ${VAR}"
        VAR_FOUND=false
        for _FLAG_ in "${_FLAG_LIST[@]}"
        do
            if [ "${_FLAG_}" == "${VAR#-}" ] 
            then
                echo -e "\e[32mFound\e[0m: ${VAR} in FLAGS."
                VAR_FOUND=true
TEST_VAL=OPT_INPUT_FLAG_${_FLAG_}
echo "TEST_VAL = ${TEST_VAL} evals to:${!TEST_VAL}"
if [ "${!TEST_VAL}" == "true" ]
then
    echo "Input required for FLAG ${_FLAG_}"
    if [ -z ${2:-""} ] || [ "$(echo ${2:-""}|cut -c1)" == "-" ]
    then
        echo "Error:  no value for ${_FLAG_}"
        echo "die missing value"
    else
        echo "Consuming value ${2}"
        eval OPT_VALUE_${_FLAG_}="${2}"
        VAL_CONSUME=true
        shift
    fi
else
    echo "No input requred: set flag"
fi
                break
            fi
        done
        for _LONG_ in "${_LONG_LIST[@]}"
        do
            if [ "${_LONG_}" == "${VAR#--}" ]
            then
                echo -e "\e[32mFound\e[0m: ${VAR} in LONG FLAGS."
                VAR_FOUND=true
TEST_VAL=OPT_INPUT_${_LONG_//-/_}
echo "TEST_VAL = ${TEST_VAL} evals to:${!TEST_VAL}"
if [ "${!TEST_VAL}" == "true" ]
then
    echo "Input required for VAR ${_LONG_}"
    if [ -z ${2:-""} ] || [ "$(echo ${2:-""}|cut -c1)" == "-" ]
    then
        echo "Error:  no value for ${_LONG_}"
        echo "die missing value"
    else
        echo "Consuming value ${2}"
        eval OPT_VALUE_${_LONG_//-/_}="${2}"
        VAL_CONSUME=true
        shift
    fi
else
    echo "No input requred: set flag"
fi
                break
            fi
        done
        if ! ${VAR_FOUND}
        then
            echo -e "\e[31m${VAR} Not Recognized\e[0m"
            Short_Usage
            exit 0
        fi
        shift
    done

}
